package com.zhang.homework.bucketsort;

import com.zhang.sort.MergeSort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhang
 * @time 2022/02/12 17:45:05
 */
public class Test {
    public static void bucketSort(int[] array) {
        List<List<Integer>> buckets = createBuckets(array, 3);
        buckets.stream().map(bucketList->(Integer[])bucketList.toArray())
                .forEach(bucketArray->MergeSort.sort(Arrays.stream(bucketArray)
                        .mapToInt(Integer::intValue).toArray()));
        System.out.println(buckets);

    }
    public static List<List<Integer>> createBuckets(int[] array, int numberOfBucket) {
        List<List<Integer>> buckets = new ArrayList<>();
        for (int i = 0; i < numberOfBucket; i++) {
            buckets.add(new ArrayList<>());
        }
        for (int i = 0; i < array.length; i++) {
            buckets.get(array[i] % numberOfBucket).add(array[i]);
        }
        return buckets;
    }

    public static void main(String[] args) {

            int[] array = {1,4,23,67,89,13,45,67,2,123,5};
            bucketSort(array);

    }
}
